<template>
  <a-divider>常规属性</a-divider>
  <a-form
    :model="element"
    :label-col="{ span: 10 }"
    :wrapper-col="{ span: 14 }"
    autocomplete="off"
    labelAlign="left"
  >
    <a-form-item label="隐藏" name="hidden" >
      <a-switch v-model:checked="element.options.hidden" />
    </a-form-item>
    <a-form-item label="标签名称" name="label" >
      <a-input v-model:value="element.options.label" size="small" />
    </a-form-item>
    <a-form-item label="标签跨距" name="labelSpan" >
      <a-input-number class="input" v-model:value="element.options.labelSpan" :min="1" :max="24" size="small" />
    </a-form-item>
    <a-form-item label="内容跨距" name="labelSpan" >
      <a-input-number class="input" v-model:value="element.options.wrapperSpan" :min="1" :max="24" size="small" />
    </a-form-item>
    <a-form-item label="字段标签对齐" name="labelAlign" >
      <a-radio-group v-model:value="element.options.labelAlign" size="small" button-style="solid">
        <a-radio-button value="left">居左</a-radio-button>
        <a-radio-button value="right">居右</a-radio-button>
      </a-radio-group>      
    </a-form-item>
    <a-form-item label="默认值" name="defaultValue" >
      <a-slider v-model:value="element.options.defaultValue" :range="element.options.range" />
    </a-form-item>
    <a-form-item label="双滑块" name="disabled" >
      <a-switch v-model:checked="element.options.range" @change="rangeChange" />
    </a-form-item>
    <a-form-item label="禁用" name="disabled" >
      <a-switch v-model:checked="element.options.disabled" />
    </a-form-item>
    <a-form-item label="标记" name="marks" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <div class="list">
        <span class="title">标记位置 : 标记名称</span>
        <div class="item" v-for="(item, index) in element.options.marksArray" :key="item.key">
          <a-input-number class="inputNumber" size="small" :value="item.key" :min="1" :max="100" style="width: 50%;" @change="(key)=>markKeyChange(key, item)" /> :
          <a-input class="inputNumber" size="small" v-model:value="item.label" style="width: 50%;" />
          <a-icon class="delete" name="MinusCircleOutlined" @click="()=>remove(index)" size="20" style="color: #ff4d4f;" />
        </div>
      </div>
      <a-button type="primary" @click="add">增加标记</a-button>
    </a-form-item>
  </a-form>
</template>

<script setup>
import { inject, reactive } from 'vue'

const WidgetData = inject('$WidgetData')
const element = reactive(WidgetData.find(WidgetData.selectKey))

const rangeChange = (range) => {
  let defaultValue = element.options.defaultValue
  if (range) {
    element.options.defaultValue = [0, defaultValue]
  } else {
    element.options.defaultValue = defaultValue[1]
  }
}

const add = () => {
  element.options.marksArray.push({
    key: '',
    label: ''
  })
}

const markKeyChange = (key, item) => {
  let f = element.options.marksArray.find(o => o.key == key)
  if (!f) {
    item.key = key
  }
}

const remove = (index) => {
  element.options.marksArray.splice(index, 1)
}

</script>

<style lang="less" scoped>
.list{
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  flex-wrap: wrap;
  width: 100%;
  .title{
    margin-right: 26px;
  }
  .item{
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .delete{
      margin-left: 6px;
      cursor: pointer;
    }
  }
}
</style>